/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bropsdox;

import client.Client;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import util.Utilities;
/**
 *
 * @author Asus
 */
public class Bropsdox {
    public static Client client;
    public static DefaultTableModel model;
    public static boolean SynAvailable = false;
    public static Thread synchroThread = new Thread() {
        @Override
        public void run() {
            long lastSync = System.currentTimeMillis();
            while (SynAvailable) {
                if (System.currentTimeMillis() - lastSync > Utilities.SYNC_PERIOD) {
                    client.synchronize();
                    lastSync = System.currentTimeMillis();
                }
            }
        }
    };
    public Bropsdox(){
        model = new DefaultTableModel(new Object [][] {},
            new String [] {
                "Name", "Path"
            });
        client = new Client(model);
    }
    
    public static void updateFiles(){
        File f = new File("files.txt");
        if(f.exists()){
            PrintWriter Pw = null;
            try {
                Pw = new PrintWriter(f);
                int rowCount = model.getRowCount();
                for(int i=0; i<rowCount;++i){
                    Pw.println(model.getValueAt(i, 0));
                    Pw.println(model.getValueAt(i, 1));
                }   
                Pw.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Bropsdox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        Bropsdox bd = new Bropsdox();
        File f = new File("files.txt");
        if(f.exists()){
             Scanner Sc = new Scanner(f);
             while (Sc.hasNextLine()) {
                String nm = Sc.nextLine();
                String dr = Sc.nextLine();
                model.addRow(new Object[]{nm,dr});
             }
             Sc.close();
        } else {
            try {
                f.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(Bropsdox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        String nd = ("D:/Marchy/Kuliah/Semester 6/IF 3038 Pemrograman Internet/Tubes 4/bropsdoxfile");
        int n = model.getRowCount();
            boolean exist = false;
            while(n>0){
                
                if(nd.equals(model.getValueAt(n-1, 0))) exist=true;
                --n;
        }
     //   File file = new File("D:/Marchy/Kuliah/Semester 6/IF 3038 Pemrograman Internet/Tubes 4/bropsdoxfile/Tubes Progin IV.pdf");
     //   model.addRow(new Object[]{nd,file});
        updateFiles();
        //jika login 
        System.out.println("nama kolom pertama " + model.getColumnName(1));
        System.out.println("jumlah data " + model.getRowCount());
        synchroThread.start();
        SynAvailable = true;
      //  client.synchronize();
    }
}
